/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view.diagram;

import java.util.List;

/**
 *
 * @author Laurent WOUTERS
 */
public class Rectangle extends Shape {
    public Rectangle(int width, int height, java.awt.Color backColor, java.awt.Color borderColor, int borderThickness, boolean isFill) {
        super(width, height, backColor, borderColor, borderThickness, isFill);
    }
    
    @Override public List<Couple> getVertices() {
        if (!isVisible()) return emptyVertices;
        return super.getVertices();
    }
    
    @Override public void paint(java.awt.Graphics2D g2d, float alpha, DiagramStyle style, Couple offset) {
        Couple r = position.plus(offset);
        g2d.setColor(getRealColor((backColor!=null)? backColor: style.shapeBackgroundColor, alpha));
        g2d.fillRect((int)r.x, (int)r.y, (int)dimension.x, (int)dimension.y);
        g2d.setColor(getRealColor((borderColor!=null)? borderColor: style.shapeBorderColor, alpha));
        g2d.setStroke(new java.awt.BasicStroke((borderThickness!=0)? borderThickness: style.shapeBorderThickness));
        g2d.drawRect((int)r.x, (int)r.y, (int)dimension.x, (int)dimension.y);
    }
    @Override protected void exportSVG(org.w3c.dom.Document doc, org.w3c.dom.Element parent, float alpha, DiagramStyle style, Couple offset) {
        if (!isVisible()) return;
        Couple r = position.plus(offset);
        org.w3c.dom.Element elem = doc.createElement("rect");
        elem.setAttribute("x", Double.toString(r.x));
        elem.setAttribute("y", Double.toString(r.y));
        elem.setAttribute("width", Double.toString(dimension.x));
        elem.setAttribute("height", Double.toString(dimension.y));
        java.awt.Color fill = getRealColor((backColor!=null)? backColor: style.shapeBackgroundColor, alpha);
        java.awt.Color border = getRealColor((borderColor!=null)? borderColor: style.shapeBorderColor, alpha);
        int stroke = (borderThickness!=0)? borderThickness: style.shapeBorderThickness;
        if (fill != null && fill.getAlpha() != 0) {
            elem.setAttribute("fill", "#" + getColorString(fill));
            if (fill.getAlpha() != 255) {
                float value = ((float)fill.getAlpha()) / 255;
                elem.setAttribute("fill-opacity", Float.toString(value));
            }
        }
        if (stroke > 0 && border != null && border.getAlpha() != 0) {
            elem.setAttribute("stroke", "#" + getColorString(border));
            if (border.getAlpha() != 255) {
                float value = ((float)border.getAlpha()) / 255;
                elem.setAttribute("stroke-opacity", Float.toString(value));
            }
            elem.setAttribute("stroke-width", Integer.toString(stroke));
        }
        parent.appendChild(elem);
    }
}
